﻿using System;
using System.Windows;
using jSLCore.Client.Presentation.ApplicationContainer;
using jSLCore.Client.Presentation.BaseControl;

namespace jSLCore.Client.Presentation.Authorization
{
    /// <summary>
    /// Control can be inserted to any other control to work with authorization (Enter,Exit, ChangePassword, see Info)
    /// </summary>
    public partial class AuthorizationInfoControl : DataControl
    {
        private readonly IPasswordManager _securityManager;

        public AuthorizationInfoControl(IPasswordManager securityManager)
        {
            _securityManager = securityManager;
            InitializeComponent();
            Loaded += (sender, args) => UpdateState(null,null);
            jSecurity.SecurityChanged += (o, eventArgs) => UpdateState(null,null);
        }



        private void enter_Clicked(object sender, RoutedEventArgs e)
        {
            ApplicationContainer.ApplicationContainer.Current.ShowAuthorizationDialog(true,UpdateState);
        }

        private void UpdateState(object sender, object data)
        {
            if(!jSecurity.IsAuthorized)
            {
                enterButton.Visibility = Visibility.Visible;
                enteredPanel.Visibility = Visibility.Collapsed;
                nameButton.Content = string.Empty;
            }
            else
            {
                enterButton.Visibility = Visibility.Collapsed;
                enteredPanel.Visibility = Visibility.Visible;
                nameButton.Content = jSecurity.Login;
            }
        }

        private void showDetail_Clicked(object sender, RoutedEventArgs e)
        {
            var dlg = new AccountDetail(_securityManager);
            dlg.ShowDialog(this);
        }

        private void logout_Clicked(object sender, RoutedEventArgs e)
        {
            ApplicationContainer.ApplicationContainer.Current.Logout(UpdateState);
            
        }
    }
}
